package barbershop.com.service.orders.impl;

import barbershop.com.dao.orders.OrderDao;
import barbershop.com.dao.orders.OrderItemDao;
import barbershop.com.entity.orders.OrderItem;
import barbershop.com.entity.orders.Orders;
import barbershop.com.service.orders.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;

@Transactional
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderItemDao orderItemDao;

    @Override
    public Orders insert(Orders record) {
        try {
            Orders order = orderDao.save(record);
            System.out.println("订单添加成功！");
            return order;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("订单添加失败！");
        }
        return null;
    }

    @Override
    public Boolean delete(Orders record) {
        try {
            this.orderDao.delete(record);
            System.out.println("订单删除成功！");
            return true;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("订单删除失败！");
            return false;
        }
    }

    @Override
    public Orders update(Orders record) {
        try {
            Orders order = this.orderDao.save(record);
            System.out.println("订单修改成功！");
            return order;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("订单修改失败！");
        }
        return null;
    }

    @Override
    public Orders find(Orders record) {
        try {
            Optional<Orders> order = this.orderDao.findById(record.getOrderId());
            if (order.isPresent()){
                System.out.println("订单查询成功！");
                return order.get();
            }
            return null;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("订单查询失败！");
        }
        return null;
    }

    @Override
    public List<Orders> findAll() {
        try {
            List<Orders> orderList = this.orderDao.findAll();
            System.out.println("所有订单查询成功！");
            return orderList;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("所有订单查询失败！");
        }
        return null;
    }

    @Override
    public List<OrderItem> findAllOrderItems(Integer orderId) {
        try {
            List<OrderItem> orderItemList = this.orderItemDao.findAllByOrdersOrderId(orderId);
            System.out.println("该订单下的所有订单条目查询成功！");
            return orderItemList;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("该订单下的所有订单条目查询失败！");
        }
        return null;
    }

    @Override
    public Integer getTotalQuantity(Integer orderId) {
        try {
            List<OrderItem> orderItemList = this.orderItemDao.findAllByOrdersOrderId(orderId);
            if (orderItemList.size()!=0){
                Integer totalQuantity = 0;
                for (OrderItem orderItem : orderItemList){
                    totalQuantity += orderItem.getQuantity();
                }
                System.out.println("该订单总数量"+totalQuantity+"件获取成功");
                return totalQuantity;
            }
            return null;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("该订单总数量获取失败");
        }
        return null;
    }

    @Override
    public Double getTotalPrice(Integer orderId) {
        try {
            List<OrderItem> orderItemList = this.orderItemDao.findAllByOrdersOrderId(orderId);
            if (orderItemList.size()!=0){
                Double totalPrice = 0.0;
                for (OrderItem orderItem : orderItemList){
                    totalPrice += orderItem.getService().getPrice();
                }
                System.out.println("该订单总价"+totalPrice+"元获取成功");
                return totalPrice;
            }
            return null;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("该订单总价获取失败");
        }
        return null;
    }

    @Override
    public void updateTotalQP(Integer orderId) {
        try {
            List<OrderItem> orderItemList = this.orderItemDao.findAllByOrdersOrderId(orderId);
            if (orderItemList.size()!=0){
                Integer totalQuantity = 0;
                Double totalPrice = 0.0;
                for (OrderItem orderItem : orderItemList){
                    totalQuantity += orderItem.getQuantity();
//                    totalPrice += orderItem.getService().getPrice();
                    totalPrice += orderItem.getSubTotalMoney();
                }
                this.orderDao.updateTotalQP(totalQuantity, totalPrice, orderId);
                System.out.println("订单字段更新成功: 总数量"+totalQuantity+
                        "件，订单总价"+totalPrice+"元");
                // 未支付状态
                this.orderDao.updateOrderStatus(0, orderId);
            }   else {
                System.out.println("订单字段更新失败！");
            }
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("订单字段更新失败！");
        }
    }

    @Override
    public void updateOrderStatus(Integer status, Integer orderId) {
        try {
            this.orderDao.updateOrderStatus(status, orderId);
            System.out.println("订单状态更新成功！");
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("订单状态更新失败！");
        }
    }

    @Override
    public List<Orders> getSuccessOrder(Integer orderStatus) {
        try {
            List<Orders> orderList = this.orderDao.findAllByOrderStatus(orderStatus);
            System.out.println("已找到所有交易成功的订单！");
            return orderList;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("未能找到所有交易成功的订单！");
        }
        return null;
    }

    @Override
    public List<Orders> findByDate(String date) {
        try {
            List<Orders> ordersList = this.orderDao.findOrdersByRecentDay(date);
            System.out.println(date+"的日期下的订单查询成功！");
            return ordersList;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println(date+"的日期下的订单查询失败！");
        }
        return null;
    }
}
